package com.orange.lockscreensettings;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;

import com.orange.lockscreensettings.common.LogHelper;

public class DetermineTheDefaultHomeActivity extends Activity implements
		OnClickListener {
	private static final String	TAG						= "DetermineTheDefaultHomeActivity";

	// private final String PKG_SYSTEM_HOMESCREEN = "com.android.launcher";
	private static final String	PKG_ORANGE_HOMESCREEN	= "com.orange.homescreen";
	private static final String	PKG_ORANGE_HOMESELECTOR	= "com.orange.homeselector";
	private static final String	CLS_ORANGE_HOMESELECTOR	= "com.orange.homeselector.HSSelector";

	private String				isFromPreSetAct;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LogHelper.d(TAG, "onCreate. ");
	}

	// @Override
	// protected void onNewIntent(Intent intent) {
	// super.onNewIntent(intent);
	// LogHelper.d(TAG, "onNewIntent. ");
	// String isFromPreSetAct = intent
	// .getStringExtra(PreferenceSettingActivity.ISFROMPRESETTINGSACT);
	// judgeWhetherToDoNext(isFromPreSetAct);
	// this.setIntent(null);
	// }

	@Override
	protected void onResume() {
		super.onResume();
		isFromPreSetAct = this.getIntent().getStringExtra(
				PreferenceSettingActivity.ISFROMPRESETTINGSACT);
		LogHelper.d(TAG, "onResume. isFromPreSetAct: " + isFromPreSetAct);
		judgeWhetherToDoNext(isFromPreSetAct);
	}

	@Override
	protected void onPause() {
		super.onPause();
		LogHelper.d(TAG, "onPause.  set isFromPreSetAct to " + isFromPreSetAct);
	}

	private void startSettingActivity() {
		Intent intent = new Intent();
		intent.setAction("com.orange.lockscreen.SETTINGS");
		startActivity(intent);
		// comment by gaoge 2011-08-24
		finish();
	}

	private void startOrangeHomeselect() {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.setClassName(PKG_ORANGE_HOMESELECTOR, CLS_ORANGE_HOMESELECTOR);
		startActivity(intent);
		finish();
	}

	private void startSelectHomeDialog() {
		// IntentFilter filter = new IntentFilter();
		// filter.addAction(Intent.ACTION_MAIN);
		// filter.addCategory(Intent.CATEGORY_HOME);
		// filter.addCategory(Intent.CATEGORY_DEFAULT);

		PackageManager manager = getPackageManager();
		Intent intent = new Intent(Intent.ACTION_MAIN, null);
		intent.addCategory(Intent.CATEGORY_HOME);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		List<ResolveInfo> list = manager.queryIntentActivities(intent, 0);
		for (int i = 0; i < list.size(); i++) {
			manager.clearPackagePreferredActivities(list.get(i).activityInfo.packageName);
		}

		Intent intent2 = new Intent(Intent.ACTION_MAIN);
		intent2.addCategory(Intent.CATEGORY_HOME);
		startActivity(intent2);
		finish();
	}

	private String getDefaultHome() {
		String defaultName = null;
		List<ComponentName> prefActList = new ArrayList<ComponentName>();
		List<IntentFilter> intentList = new ArrayList<IntentFilter>();
		getPackageManager().getPreferredActivities(intentList, prefActList,
				null);
		int actSize = prefActList.size();
		if (actSize > 0) {
			int filterSize = intentList.size();
			for (int i = 0; i < filterSize; i++) {
				if (Intent.CATEGORY_HOME.equals(intentList.get(i)
						.getCategory(0))) {
					defaultName = prefActList.get(i).getPackageName();
					break;
				}
			}
		}
		return defaultName;
	}

	// add by gaoge 2011-08-24 for fix bug 0013273
	private void judgeWhetherToDoNext(String isFromPreSetAct) {
		boolean isFromHome = this.getIntent()
				.getBooleanExtra("fromHome", false);
		LogHelper.d(TAG, "############################## isFromHome: "
				+ isFromHome);
		if (null != isFromPreSetAct && isFromPreSetAct.equals("true")) {
			this.finish();
			LogHelper.d(TAG, TAG + " finished. ");
			return;
		} else {
			String defaultHomeName = getDefaultHome();
			if (defaultHomeName == null) {
				startSettingActivity();
				LogHelper.d(TAG,
						"judgeWhetherToDoNext. defaultHomeName is null.");
			} else {
				if (defaultHomeName.equals(PKG_ORANGE_HOMESCREEN)) {
					LogHelper.d(TAG,
							"judgeWhetherToDoNext. default ls is orange");
					startSettingActivity();
				} else {
					// if (SettingsUtils.isPackageExist(
					// DetermineTheDefaultHomeActivity.this,
					// PKG_ORANGE_HOMESELECTOR)) {
					ShowDialog();
					// } else {
					// startSelectHomeDialog();
					// }
				}
			}
		}
	}

	private void ShowDialog() {
		Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.homescreen_settings);
		builder.setIcon(R.drawable.ic_dialog_menu_generic);
		builder.setMessage(R.string.these_settings_are_only_available);
		builder.setPositiveButton(R.string.close, this);
		Dialog dialog = builder.show();

		// WindowManager.LayoutParams windowParams = dialog.getWindow()
		// .getAttributes();
		// windowParams.width = 300;
		// windowParams.height = 200;
		// dialog.getWindow().setAttributes(windowParams);

		// Display d = m.getDefaultDisplay();
		// LayoutParams p = getWindow().getAttributes();
		// p.height = (int) (d.getHeight() * 0.6);
		// p.width = (int) (d.getWidth() * 0.95);

	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		switch (which) {
		case Dialog.BUTTON_POSITIVE:
			// startOrangeHomeselect();
			startSelectHomeDialog();
			break;
		case Dialog.BUTTON_NEGATIVE:
			finish();
			break;
		default:
			break;
		}
	}

}
